<?php

class SitesController extends AppController {

    var $name = 'Sites';
    var $uses = array('Site', 'SiteUser', 'CustProductSite', 'SiteCostCenter');

    function option_list($customer_id) {
        $sites = $this->Site->find('list', array('conditions' => array('customer_id' => $customer_id)));
        parent::option_list($sites);
    }

    function option_list_cost_center($site_id) {
        $data = $this->SiteCostCenter->find('list', array('conditions' => array('site_id' => $site_id)));
        parent::option_list($data);
    }

    function site_list($customer_id) {
        $sites = array();
        if (isset($this->passedArgs['allOption']) && $this->passedArgs['allOption'] == true) {
            $sites[''] = 'All';
        }
//        // ถ้าเป็น Vendor ให้เห็นเฉพาะ Site ที่มีสิทธิ์เท่านั้น
//        if ($this->Auth->user('prefix') == SUPPLIER_PREFIX || $this->Session->read('Auth.User.Extend.from_supplier') == true) {
//            $s = $this->CustProductSite->listSiteBySupplierId($this->Auth->user('supplier_id'), $customer_id);
//            $sites[] = $this->Site->find('list', array('conditions' => array('id' => $s)));
//        } else {
        $sites[] = $this->Site->find('list', array('conditions' => array('customer_id' => $customer_id)));
//        }
        return $sites;
    }

    function site_user_list($site_cost_center_id) {
        $sites = $this->SiteUser->find('list', array('conditions' => array('site_cost_center_id' => $site_cost_center_id)));
        parent::option_list($sites);
    }

    function autocomplete_site_users() {
        //Partial strings will come from the autocomplete field as
        //$this->data['Post']['subject']
        $sites = $this->SiteUser->find('list', array(
                    'conditions' => array(
                        'site_cost_center_id' => $site_cost_center_id,
                        'name LIKE' => $this->data['CustStockOutHead']['site_user_name']
                        )));
        parent::autocomplete($sites);
    }

    /**
     * Check ว่า Site ที่กำลังดูอยู่นี้สามารถ Reserve Stock out ได้หรือไม่
     * @return <type>
     */
    function has_cust_reserve_stock_cout() {
        $this->Site->id = $this->Auth->user('site_id');
        if ($this->Site->field('can_reserve_stock_out') == true) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check ว่า Site ที่กำลังดูอยู่นี้มี Cost Center ด้วยหรือไม่
     * @return <type>
     */
    function has_site_cost_center() {
        $this->Site->id = $this->Auth->user('site_id');
        if ($this->Site->field('have_cost_center') == true) {
            return true;
        } else {
            return false;
        }
    }

}

?>
